package com.neusoft.yiyang.diet.controller;

import com.neusoft.yiyang.common.entity.diet.MealRecord;
import com.neusoft.yiyang.common.entity.diet.vto.FoodIdQuantity;
import com.neusoft.yiyang.common.entity.diet.vto.FoodItemDetail;
import com.neusoft.yiyang.diet.service.MealRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Tag(name = "餐食记录管理接口", description = "负责客户餐食记录的添加、查询等操作，支持批量处理")
@RestController
@RequestMapping("/meal-records")
public class MealRecordController {

    @Autowired
    private MealRecordService mealRecordService;

    @Operation(summary = "添加客户餐食记录", description = "为指定客户添加特定日期的餐食记录（包含各餐次的食材及用量）")
    @PostMapping("/add")
    public void addMealRecord(
            @Parameter(description = "客户餐食数据（结构：{客户ID: {餐次: [{食材ID: xxx, 用量: xxx}, ...]}}）", required = true)
            @RequestBody Map<String, Map<String, List<FoodIdQuantity>>> customerFoods,
            @Parameter(description = "餐食日期（格式：yyyy-MM-dd）", required = true)
            @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
            @Parameter(description = "客户ID", required = true)
            @RequestParam("customerId") Integer customerId,
            @Parameter(description = "操作人ID（可选，如护工ID）")
            @RequestParam(value = "userId", required = false) Integer userId) {
        mealRecordService.addMealRecord(customerFoods, date, customerId, userId);
    }

    @Operation(summary = "批量添加套餐记录", description = "为多个客户添加特定日期的套餐餐食记录（按类别区分套餐）")
    @PostMapping("/addPackage")
    public void addPackage(
            @Parameter(description = "套餐餐食数据（结构：{客户ID: {餐次: [{食材ID: xxx, 用量: xxx}, ...]}}）", required = true)
            @RequestBody Map<String, Map<String, List<FoodIdQuantity>>> customerFoods,
            @Parameter(description = "套餐日期（格式：yyyy-MM-dd）", required = true)
            @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
            @Parameter(description = "套餐类别（如早餐/午餐/晚餐）", required = true)
            @RequestParam("category") String category) {
        mealRecordService.addPackage(customerFoods, date, category);
    }

    @Operation(summary = "查询客户餐食记录", description = "根据餐食明细和日期查询指定客户的餐食记录（含食材用量）")
    @PostMapping("/getFoodRecord")
    public Map<String, Map<String, List<FoodIdQuantity>>> getFoodRecord(
            @Parameter(description = "餐食明细数据（结构：{餐次: {食材明细}}）", required = true)
            @RequestBody Map<String, Map<String, FoodItemDetail>> mealData,
            @Parameter(description = "查询日期（格式：yyyy-MM-dd）", required = true)
            @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
            @Parameter(description = "客户ID（可选，不填则查询所有客户）")
            @RequestParam(value = "customerId", required = false) Integer customerId) {
        return mealRecordService.getFoodRecord(mealData, date, customerId);
    }

    @Operation(summary = "查询套餐餐食记录", description = "根据套餐明细和日期查询指定类别的套餐餐食记录")
    @PostMapping("/getFoodRecordPackage")
    public Map<String, Map<String, List<FoodIdQuantity>>> getFoodRecordPackage(
            @Parameter(description = "套餐明细数据（结构：{餐次: {食材明细}}）", required = true)
            @RequestBody Map<String, Map<String, FoodItemDetail>> mealData,
            @Parameter(description = "查询日期（格式：yyyy-MM-dd）", required = true)
            @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
            @Parameter(description = "套餐类别（可选，不填则查询所有类别）")
            @RequestParam(value = "category", required = false) String category) {
        return mealRecordService.getFoodRecordPackage(mealData, date, category);
    }

    @Operation(summary = "获取所有餐食记录", description = "查询系统中所有客户的餐食记录列表")
    @GetMapping("/all")
    public List<MealRecord> getAllMealRecords() {
        return mealRecordService.getAllMealRecords();
    }

    @Operation(summary = "查询客户的餐食记录", description = "获取指定客户的所有餐食记录")
    @GetMapping("/customer/{customerId}")
    public List<MealRecord> getMealRecordsByCustomerId(
            @Parameter(description = "客户ID", required = true)
            @PathVariable Integer customerId) {
        return mealRecordService.getMealRecordsByCustomerId(customerId);
    }
}